//
//  ViewController.m
//  LoginView
//
//  Created by apple on 16/1/24.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisViewController.h"
#import "HomeViewController.h"
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size

@interface LoginViewController ()
{
    UITextField * _loginText;
    UITextField * _passwdText;
}
@end

@implementation LoginViewController

- (void)viewDidLoad {
    //创建用户名和密码框
    _loginText = [[UITextField alloc]initWithFrame:CGRectMake(20, 80, SCREEN_SIZE.width-40, 30)];
    _loginText.borderStyle = UITextBorderStyleRoundedRect;
    _loginText.placeholder = @"请输入用户名";
    UIImageView * loginImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    loginImage.image = [UIImage imageNamed:@"login_user"];
    _loginText.leftView =loginImage;
    _loginText.leftViewMode = UITextFieldViewModeAlways;
    _passwdText = [[UITextField alloc]initWithFrame:CGRectMake(20, 130, SCREEN_SIZE.width-40, 30)];
    _passwdText.borderStyle = UITextBorderStyleRoundedRect;
    _passwdText.placeholder = @"请输入密码";
    UIImageView * passwdImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    passwdImage.image = [UIImage imageNamed:@"login_pwdico"];
    _passwdText.leftView =passwdImage;
    _passwdText.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_loginText];
    [self.view addSubview:_passwdText];
    
    //创建登录按钮和注册按钮
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(SCREEN_SIZE.width/4-50, 180, 100, 30);
    [btn setTitle:@"登录" forState:UIControlStateNormal];
    btn.layer.masksToBounds=YES;
    btn.layer.cornerRadius = 10;
    btn.backgroundColor = [UIColor cyanColor];
    [btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(SCREEN_SIZE.width/4*3-50, 180, 100, 30);
    [btn2 setTitle:@"注册" forState:UIControlStateNormal];
    btn2.layer.masksToBounds=YES;
    btn2.layer.cornerRadius = 10;
    btn2.backgroundColor = [UIColor cyanColor];
    [btn2 addTarget:self action:@selector(regis) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.view addSubview:btn2];
    
    _loginText.text=@"a";
    _passwdText.text=@"a";
}
-(void)regis{
    RegisViewController * con = [[RegisViewController alloc]init];
    [self presentViewController:con animated:YES completion:nil];
}


-(void)login{
    if (_loginText.text.length==0) {
        UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请输入用户名" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertCon addAction:action];
        [self presentViewController:alertCon animated:YES completion:nil];
        return;
    }
    if (_passwdText.text.length==0) {
        UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请输入密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertCon addAction:action];
        [self presentViewController:alertCon animated:YES completion:nil];
        return;
    }
    
    
    [self loginServer];
}

-(void)loginServer{
//    NSDictionary *params = @{
//                             @"username" : _loginText.text,
//                             @"password" : _passwdText.text
//                             };
    NSDictionary *params = @{
                             @"username" : @"666666",
                             @"password" : @"666666"
                             };
    [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    [HttpTool post:@"auth/oauth/token" params:params success:^(id responseObj) {
        NSLog(@"");
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertCon addAction:action];
        [self presentViewController:alertCon animated:YES completion:nil];
        
            HomeViewController * homeVC = [[HomeViewController alloc]init];
        RTRootNavigationController *nav = [[RTRootNavigationController alloc] initWithRootViewController:homeVC];
        // 添加为子控制器
        nav.title = @"任务箱";
        [UIApplication sharedApplication].delegate.window.rootViewController=nav;
//        rootViewController = nav;
    } failure:^(NSError *error) {
//        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
//        [UITool showMBProgressHud:[UIApplication sharedApplication].keyWindow withText:@"登录失败，稍后再试" withTime:1.0];
        
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
//        UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        [alertCon addAction:action];
//        [self presentViewController:alertCon animated:YES completion:nil];
//        
//        HomeViewController * homeVC = [[HomeViewController alloc]init];
//        RTRootNavigationController *nav = [[RTRootNavigationController alloc] initWithRootViewController:homeVC];
//        // 添加为子控制器
//        nav.title = @"任务箱";
//        
//        [UIApplication sharedApplication].delegate.window.rootViewController=nav;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
